package huffmanTree;

import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;

import java.util.Iterator;

/**
 * @author Aaron
 * @desc
 * @createTime 2021/12/11 19:52
 * @since
 */
public class ByteUtil {

    public static byte[] getByteByString(String binaryString){
        Iterable iterable = Splitter.fixedLength(8).split(binaryString);
        byte[] ret = new byte[Iterables.size(iterable) ];
        Iterator iterator = iterable.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            String value = iterator.next().toString();
            Integer byteAsInt = null;
            byteAsInt = Integer.parseInt(value, 2);
            /**
             * 最后不足8位的往前移动
             */
            byte bytes = byteAsInt.byteValue();
            if (value.length()<8){
                bytes = (byte) (bytes<<(8-value.length()));
            }
            ret[i]=bytes;
            i++;
        }
        return ret;
    }

    public static boolean isTrueAtBitIndex(byte value,int bitIndex){
        bitIndex = getByteMode(bitIndex);
        return (value & (1<<(7-bitIndex)))>0;
    }

    public static String byteToString(byte byets){
        return Integer.toBinaryString((byets & 0xFF) + 0x100).substring(1);
    }

    public static byte combineBytes(byte left,byte right,int bitIndex){
        /**
         * 如果bitIndex>=8则取除以8的余数
         */
        bitIndex = getByteMode(bitIndex);
        /**
         * 如果bitindex是0则返回左边的byte
         */
        if (getByteRemainder(bitIndex) == 0){
            return left;
        }
        /**
         * 如果bitIndex是3，则左byte左移3位占据高位 右字节右移5位让高位的3位占据低位
         * 两者相加，则两个数组的首位相连重新组成一个新的byte
         */
        return (byte)(((left<<bitIndex) & 0xff)+(((right&0xff)>>(8-bitIndex)) & 0xff));
    }

    public static int getByteMode(int bitIndex) {
        if ((bitIndex<<3)>0){
            bitIndex = getByteRemainder(bitIndex);
        }
        return bitIndex;
    }

    public static byte splitByte(byte value,int fromIndex,int endIndex){
        if (fromIndex>endIndex){
            throw new ArrayIndexOutOfBoundsException("endIndex could not be bigger than fromIndex,fromIndex:"+fromIndex+",endIndex:"+endIndex);
        }
        fromIndex = getByteMode(fromIndex);
        endIndex = getByteMode(endIndex);
        int result;
        /**
         * byte左移fromIndex位 endIndex相应递减
         */
        result = value << fromIndex;
        endIndex = endIndex - fromIndex;
        /**
         * 如果此时的endIndex是2，右边是如何组装11100000的数据
         * 10101010   00000001
         * &          <<5
         * 11100000  ----------
         * ---------  00100000
         * 10100000  -1
         *           ----------
         *           00011111
         *           ~ 取反
         *           ------------
         *           11100000
         */
        return (byte)(result & (~((1<<(7-endIndex))-1)));
    }

    /**
     * #@desc 获取一个数除以8的余数
     * @param bitIndex
     * @return
     */
    public static int getByteRemainder(int bitIndex) {
        return bitIndex & 7;
    }

    public static void main(String[] args) {
        String a="11011111";
        byte[] byteByString = getByteByString(a);
        byte b = byteByString[0];
        int c=(Integer.parseInt(Integer.toBinaryString((b & 0xFF) + 0x100).substring(1), 2))>>1;
        System.out.println(Integer.toBinaryString((c & 0xFF) + 0x100).substring(1));
    }
}
